package com.hiultra.archive.service.impl;

import com.hiultra.archive.domain.ArcBorrow;
import com.hiultra.archive.domain.ArcBorrowDetails;
import com.hiultra.archive.domain.ArcBorrowReturn;
import com.hiultra.archive.domain.BorrowConstant;
import com.hiultra.archive.mapper.ArcBorrowMapper;
import com.hiultra.archive.service.IArcBorrowService;
import com.hiultra.common.utils.DateUtils;
import com.hiultra.common.utils.OrderNoUtil;
import com.hiultra.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 借阅Service业务层处理
 *
 * @author ls
 * @date 2022-07-28
 */
@Service
public class ArcBorrowServiceImpl implements IArcBorrowService {
    @Autowired
    private ArcBorrowMapper arcBorrowMapper;

    /**
     * 查询借阅
     *
     * @param borrowId 借阅主键
     * @return 借阅
     */
    @Override
    public ArcBorrow selectArcBorrowByBorrowId(Long borrowId) {
        return arcBorrowMapper.selectArcBorrowByBorrowId(borrowId);
    }

    /**
     * 查询借阅列表
     *
     * @param arcBorrow 借阅
     * @return 借阅
     */
    @Override
    public List<ArcBorrow> selectArcBorrowList(ArcBorrow arcBorrow) {
        return arcBorrowMapper.selectArcBorrowList(arcBorrow);
    }

    /**
     * 新增借阅
     *
     * @param arcBorrow 借阅
     * @return 结果
     */
    @Transactional
    @Override
    public int insertArcBorrow(ArcBorrow arcBorrow) {
        // 初始化数据
        arcBorrow.setDelflag(BorrowConstant.DEL_STATUS_NORMAL);
        arcBorrow.setStatus(BorrowConstant.STATUS_APPROVE_WAIT);
        arcBorrow.setBorrowNumber(OrderNoUtil.createBorrowNo());

        int rows = arcBorrowMapper.insertArcBorrow(arcBorrow);

        insertArcBorrowDetails(arcBorrow);
        return rows;
    }

    /**
     * 修改借阅
     *
     * @param arcBorrow 借阅
     * @return 结果
     */
    @Transactional
    @Override
    public int updateArcBorrow(ArcBorrow arcBorrow) {
        arcBorrow.setUpdateTime(DateUtils.getNowDate());
        arcBorrowMapper.deleteArcBorrowDetailsByBorrowId(arcBorrow.getBorrowId());
        insertArcBorrowDetails(arcBorrow);
        return arcBorrowMapper.updateArcBorrow(arcBorrow);
    }

    /**
     * 批量删除借阅
     *
     * @param borrowIds 需要删除的借阅主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteArcBorrowByBorrowIds(Long[] borrowIds) {
        arcBorrowMapper.deleteArcBorrowDetailsByBorrowIds(borrowIds);
        return arcBorrowMapper.deleteArcBorrowByBorrowIds(borrowIds);
    }

    /**
     * 删除借阅信息
     *
     * @param borrowId 借阅主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteArcBorrowByBorrowId(Long borrowId) {
        arcBorrowMapper.deleteArcBorrowDetailsByBorrowId(borrowId);
        return arcBorrowMapper.deleteArcBorrowByBorrowId(borrowId);
    }

    /**
     * 新增借阅详情信息
     *
     * @param arcBorrow 借阅对象
     */
    public void insertArcBorrowDetails(ArcBorrow arcBorrow) {
        List<ArcBorrowDetails> arcBorrowDetailsList = arcBorrow.getArcBorrowDetailsList();
        Long borrowId = arcBorrow.getBorrowId();
        if (StringUtils.isNotNull(arcBorrowDetailsList)) {
            List<ArcBorrowDetails> list = new ArrayList<ArcBorrowDetails>();
            for (ArcBorrowDetails arcBorrowDetails : arcBorrowDetailsList) {
                arcBorrowDetails.setBorrowId(borrowId);
                list.add(arcBorrowDetails);
            }
            if (list.size() > 0) {
                arcBorrowMapper.batchArcBorrowDetails(list);
            }
        }
    }

    /**
     * 借阅归还
     *
     * @param arcBorrowReturn
     * @return
     */
    @Transactional
    @Override
    public int returnArcBorrowByDetailIds(ArcBorrowReturn arcBorrowReturn) {
        int rows = arcBorrowMapper.returnArcBorrowDetails(arcBorrowReturn.getDetailIds());
        ArcBorrow arcBorrow = arcBorrowMapper.selectArcBorrowByBorrowId(arcBorrowReturn.getBorrowId());

        if (rows == arcBorrow.getArcBorrowDetailsList().size()) {
            arcBorrowReturn.setStatus(ArcBorrow.STATUS_ALL_RETURN);
        } else {
            arcBorrowReturn.setStatus(ArcBorrow.STATUS_PART_RETURN);
        }
        return arcBorrowMapper.returnArcBorrow(arcBorrowReturn);
    }

}
